package com.klod.news.ext



import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier

import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.klod.news.data.NetworkError
import com.klod.news.data.NetworkResult
import com.klod.news.data.NetworkStateScope
import com.klod.news.widget.ErrorView
import com.klod.news.widget.LottieLoading
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.MutableStateFlow

import kotlinx.coroutines.launch

fun ViewModel.launch(block: suspend () -> Unit) =
    viewModelScope.launch {
        block()
    }


/***
 * 网络请求状态解析
 * @param retry 失败重试
 * @param defaultLoading 默认加载中Composable
 * @param defaultError 默认错误Composable
 * @param scopeConfiguration 协程作用域配置 状态Composable
 * @param loadingModifier 加载中Composable样式
 * @param errorModifier 加载失败Composable样式
 */
@Composable
fun <T> NetworkResult<T>.parseState(
    loadingModifier: Modifier = Modifier.fillMaxWidth(),
    errorModifier: Modifier = Modifier.fillMaxWidth(),
    retry: () -> Unit = {},
    defaultLoading: @Composable () -> Unit = { LottieLoading(modifier = loadingModifier) },
    defaultError: @Composable (String,()-> Unit) -> Unit = { msg,func -> ErrorView(msg,func, modifier = errorModifier) },
    scopeConfiguration: NetworkStateScope<T>.() -> Unit
) {
    val scope = NetworkStateScope<T>().apply(scopeConfiguration)

    when (this) {
        is NetworkResult.Loading -> {
            scope.loadingComposable?.invoke() ?: defaultLoading()
        }
        is NetworkResult.Success -> {
            // 注意：这里需要确保 scope.successComposable 不为 null
            // 如果允许不定义 onSuccess，则需要有相应的默认处理或错误提示
            scope.successComposable?.invoke(this.data)?: defaultLoading()

        }
        is NetworkResult.Error -> {
            val errorMessage = this.exception.message // 从 NetworkResult.Error 中获取错误信息
            scope.errorComposable?.invoke(errorMessage,retry) ?: defaultError(errorMessage,retry)
        }
        // 如果你的 NetworkResult 有其他状态，可以在这里处理
        // else -> {
        //     scope.otherComposable?.invoke() ?: Text("未处理的网络状态", modifier = modifier)
        // }
    }
}
//@Composable
//fun <T> NetworkResult<T>.parseState(
//    modifier: Modifier = Modifier,
//    onError: @Composable (msg:String) -> Unit = {msg-> ErrorView(msg, modifier = modifier) },
//    onLoading: @Composable () -> Unit = { LottieLoading(modifier = modifier) },
////    other: @Composable () -> Unit = { ErrorView("未知错误", modifier = modifier) },
//    onSuccess: @Composable (T) -> Unit,
//) {
//
//    when (this) {
//        is NetworkResult.Success -> onSuccess(this.data)
//        is NetworkResult.Error -> onError(this.exception.message)
//        is NetworkResult.Loading -> onLoading()
////        else -> other()
//    }
//}

fun <T> Flow<T>.mutableStateIn(
    scope: CoroutineScope,
    initialValue: T
): MutableStateFlow<T> {
    val flow = MutableStateFlow(initialValue)

    scope.launch {
        this@mutableStateIn.collect(flow)
    }

    return flow
}

